﻿@{
    ViewData["Title"] = "修改物料入库单";
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改物料入库单</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form layui-form-pane" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>入库单编号</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="recptCode" autocomplete="off" placeholder="请输入入库单编号" lay-verify="required|recptCode" class="layui-input" id="recptCode">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:90px;">自动生成</label>
                <div class="layui-input-inline" style="width: 80px;">
                    <input type="checkbox" name="switch" lay-skin="switch" lay-text="OFF|ON" lay-filter="switchFilter">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>入库单名称</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="recptName" autocomplete="off" placeholder="请输入入库单名称" lay-verify="required|recptName" class="layui-input" id="recptName">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>入库日期</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="recptDate" id="recptDate" lay-verify="required|recptDate" placeholder="请选择入库日期" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">单据状态</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <select name="status" lay-filter="aihao" disabled>
                        <option value=""></option>
                        <option selected id="PREPARE" value="PREPARE">草稿</option>
                        <option id="Completed" value="Completed">已完成</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span style="color:red">*</span>供应商</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="vendorName" autocomplete="off" placeholder="请选择供应商" lay-verify="required|vendorName" class="layui-input" id="vendorName">
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 80px;">
                        <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-search" id="openWindow">搜索</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">入库仓库</label>
                <div class="layui-input-inline" style="width: 180px;">
                    <input type="text" name="warehouseName" autocomplete="off" placeholder="请选择入库仓库" lay-verify="warehouseName" class="layui-input" id="warehouseName">
                </div>
            </div>

        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark" id="remark"></textarea>
            </div>
        </div>
        @* <div style="border-bottom: 1px solid black;"><h3 style="text-align:center;">物料信息</h3></div><br />
        <button type="button" class="layui-btn layui-btn-primary layui-bg-blue" id="addData"> 添加</button>
        <table class="layui-hide" id="test"></table> *@
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="demo3">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
        </div>
    </form>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script> 
            //JavaScript代码
            layui.use(function () {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var ids= parent.recptCode ;
            //显示
            function show() {
                  table.render({
                    elem: '#test',
                    url: '/Song/GetLineList?code='+ids,
                    //maxHeight: '530px',
                    //toolbar: '#TPL-treeTable-demo',
                     // page: true, //开启分页
                     // limit: 10,
                     // limits: [10, 20, 50, 100],
                    cols: [[
                        { field: 'recptCode', title: '物料编码', width: 150, fixed: 'left' },
                        { field: 'recptName', title: '物料名称', width: 120 },
                        { field: 'vendorName', title: '规格型号', width: 150 },
                        { field: 'vendorName', title: '单位', width: 150 },
                        { field: 'vendorName', title: '入库数量', width: 150 },
                        { field: 'vendorName', title: '批次号', width: 150 },
                        { field: 'vendorName', title: '仓库', width: 150 },
                        { field: 'vendorName', title: '库区', width: 150 },
                        { field: 'vendorName', title: '库位', width: 150 },
                        {
                            field: 'recptDate', title: '有效期', width: 150, templet: function (row) {
                                if (row.recptDate == null) {
                                    return "";
                                }
                                else {
                                    var tmp = row.recptDate.split("T")[0];
                                    return tmp;
                                }
                            }
                        },
                        { field: 'poCode', title: '检验单编号', width: 150 },
                        {
                            field: 'status', title: '是否检验', width: 140, templet: function (row) {
                                var tmp = row.status == 'PREPARE' ? "草稿" : "已完成";
                                return tmp;
                            }
                        }, 
                        {
                            fixed: "right", title: "操作", width: 150, align: "center", templet: function (row) {
                                if (row.status == 'PREPARE') {
                                    var tmp = '<a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="detail">查看</a>';
                                    tmp += '<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delChild">删除</a>';
                                }else{
                                    var tmp = '';
                                }
                               
                                return tmp;
                            }
                        }
                    ]],
                });
            }

             // 添加分类
                    $("#addData").click(function () {
                      layer.open({
                        type: 2,
                    title: '添加物料入库单行',
                         shadeClose: true, // 点击遮罩区域，关闭弹层
                          scrollbar: false, // 暂时屏蔽浏览器滚动条
                        skin: 'layui-layer-rim', //加上边框
                        area: ['90%', '90%'], //宽高
                    content: '/Song/AddWmItemRecptLine',
                        end: function () {
                            show();//刷新父级页面列表方法，类似于回调函数
                        }
                    });
                    });







            show();

          form.on('switch(switchFilter)', function(data) {
            if(data.elem.checked) {
              var prefix ='R';
              var num = new Date().getTime(); // 使用当前时间戳生成数字部分
              document.getElementById('recptCode').value = prefix + num;
            } else {
              document.getElementById('recptCode').value = '';
            }
          });

          // 日期
                laydate.render({
                    elem: '#recptDate'
                });

           // 供应商选择
        document.getElementById('openWindow').onclick = function(){
            // 弹出新窗口
            layer.open({
                type: 2, // 弹出层类型，2表示iframe层
                title: '供应商选择',
                  shadeClose: true, // 点击遮罩区域，关闭弹层
                              scrollbar: false, // 暂时屏蔽浏览器滚动条
                area: ['90%', '90%'], // 弹出层大小
                    content: '/Song/SupplierManagements', // 弹出层内容，供应商选择页面地址
                    end: function () {
                        var vendorNames = sessionStorage.getItem('vendorNameFromPopup');
                        document.getElementById('vendorName').value = vendorNames;
                    }
            });
        };



                // 表单验证
                form.verify({
                    // 入库单编号
                    recptCode: function (value) {
                        if (value.length > 20) {
                            return '入库单编号不能超过20个字符';
                        }
                        // 入库单编号不能包含特殊字符
                        var reg = /[`~!#$%^&*()_+<>?:"{},.\/;'[\]]/im;
                        if (reg.test(value)) {
                            return '入库单编号不能包含特殊字符';
                        }
                    },
                // 入库单名称
                recptName: function (value) {
                    if (value.length > 20) {
                        return '入库单名称不能超过20个字符';
                    }
                    // 入库单名称不能包含特殊字符
                    var reg = /[`~!#$%^&*()_+<>?:"{},.\/;'[\]]/im;
                    if (reg.test(value)) {
                        return '入库单名称不能包含特殊字符';
                    }
                },
                // 入库日期 验证日期格式
                recptDate: function (value) {
                    if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
                        return '日期格式不正确，应为yyyy-mm-dd';
                    }
                },
                // 供应商
                vendorName: function (value) {
                    if (value.length > 20) {
                        return '供应商名称不能超过20个字符';
                    }
                    // 供应商名称不能包含特殊字符
                    var reg = /[`~!#$%^&*()_+<>?:"{},.\/;'[\]]/im;
                    if (reg.test(value)) {
                        return '供应商名称不能包含特殊字符';
                    }
                },
                // 入库仓库
                warehouseName: function (value) {
                    if (value.length > 20) {
                        return '入库仓库名称不能超过20个字符';
                    }
                    // 入库仓库名称不能包含特殊字符
                    var reg = /[`~!#$%^&*()_+<>?:"{},.\/;'[\]]/im;
                    if (reg.test(value)) {
                        return '入库仓库名称不能包含特殊字符';
                    }
                }
                });

                 function getchildIframeData() {
                 var form = layui.form;
                $("#recptCode").val(parent.recptCode);
                $("#recptName").val(parent.recptName);
                $("#recptDate").val(parent.recptDate.split("T")[0]);
                $("#vendorName").val(parent.vendorName);
                $("#remark").val(parent.remark);
                $("#warehouseName").val(parent.warehouseName);
                form.render();//重新渲染表单
            }
            getchildIframeData()
                 // areaCode = data.areaCode;
                 //    areaId = data.areaId;
                 //    areaName = data.areaName;
                 //    charger = data.charger;
                 //    createTime = data.createTime;
                 //    id = data.id;
                 //    iqcCode = data.iqcCode;
                 //    iqcId = data.iqcId;
                 //    locationCode = data.locationCode;
                 //    locationId = data.locationId;
                 //    locationName = data.locationName;
                 //    poCode = data.poCode;
                 //    recptCode = data.recptCode;
                 //    recptDate = data.recptDate;
                 //    recptName = data.recptName;
                 //    remark = data.remark;
                 //    status = data.status;
                 //    updateBy = data.updateBy;
                 //    updateTime = data.updateTime;
                 //    vendorCode = data.vendorCode;
                 //    vendorId = data.vendorId;
                 //    vendorName = data.vendorName;
                 //    vendorNick = data.vendorNick;
                 //    warehouseCode = data.warehouseCode;
                 //    warehouseId = data.warehouseId;
                 //    warehouseName = data.warehouseName;

                // 修改事件
                form.on('submit(demo3)', function (data) {
                    var field = data.field; // 获取表单字段值
                var updDTO = {
                    recptCode: field.recptCode,
                    recptName: field.recptName,

                    vendorId: sessionStorage.getItem("vendorIdFromPopup"),
                    vendorCode: sessionStorage.getItem("vendorCodeFromPopup"),
                    vendorName: sessionStorage.getItem("vendorNameFromPopup"),
                    vendorNick: sessionStorage.getItem("vendorNickFromPopup"),

                    warehouseName: field.warehouseName,
                    Id: parent.id ,
                    poCode: parent.poCode,
                    recptDate: field.recptDate,
                    status: field.status,
                    remark: field.remark,
                }
                         // 此处可执行 Ajax 等操作
                $.post("/Song/UpdateWmItemRecpt", updDTO, res => {
                        if (res) {
                            layer.msg('保存成功', {
                                icon: 1,
                                time: 2000
                            }, function () {
                                var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
                                parent.layer.close(index);//再执行关闭
                            });
                        }
                        else {
                            layer.msg('网络异常', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    return false; // 阻止默认 form 跳转
                });
            });
    </script>
</body>
</html>
